﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.Colors;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.DrawingStandard
{
    public static class PLPloter
    {
        /// <summary>
        /// 添加多线
        /// </summary>
        /// <param name="sp"></param>
        /// <param name="ep"></param>
        /// <param name="layerName">图层名称</param>
        /// <param name="dx0">起点X偏移</param>
        /// <param name="dy0">起点X偏移</param>
        /// <param name="dx1">终点X偏移</param>
        /// <param name="dy1">终点Y偏移</param>
        /// <param name="num"></param>
        /// <returns></returns>
        public static void AddPolyline(Database db,Point2d sp, Point2d ep,ref Extents2d ext, string layerName, double dx0=0, double dy0=0, double dx1=0, double dy1=0)
        {
            //DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;
        
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline line = new Polyline() { Closed = false, LayerId = lt[layerName], Plinegen = true };//定义不封闭的Polyline
                line.AddVertexAt(0, sp.C2D(dx0, dy0), 0, 0, 0);
                line.AddVertexAt(1, ep.C2D(dx1, dy1), 0, 0, 0);

                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }
          
        }

        /// <summary>
        /// 创建多线
        /// </summary>
        /// <param name="stPoint">起点</param>
        /// <param name="endPoint">终点</param>
        /// <param name="layer">图层</param>
        /// <returns></returns>
        public static void AddPolyline(Database db, Point2d stPoint, Point2d endPoint,ref Extents2d ext, string layer)
        {

            //Database db = HostApplicationServices.WorkingDatabase;
          
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline line = new Polyline() { LayerId = lt[layer], Plinegen = true };
                line.AddVertexAt(0, stPoint.C2D(), 0, 0, 0);
                line.AddVertexAt(1, endPoint.C2D(), 0, 0, 0);
                line.LineWeight = LineWeight.LineWeight009;

                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }
            
        }

        public static void AddBoldPolyline(Database db,Point2d stPoint, Point2d endPoint, ref Extents2d ext, string layer,double lineW=5)
        {

            //Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline line = new Polyline() { LayerId = lt[layer], Plinegen = true };
                line.AddVertexAt(0, stPoint.C2D(), 0, lineW, lineW);
                line.AddVertexAt(1, endPoint.C2D(), 0, lineW, lineW);
                //line.LineWeight = LineWeight.LineWeight030;

                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }

        }
        /// <summary>
        /// 根据点集合绘制多线
        /// </summary>
        /// <param name="ptList">点集合</param>
        /// <param name="layerName">图层名称</param>
        /// <param name="isClose">是否闭合，默认不闭合</param>
        /// <returns></returns>
        public static void AddPolylineByPointList(Database db, List<Point2d> ptList,ref Extents2d ext, string layerName, bool isClose)
        {
            DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;
           
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline line = new Polyline() { Closed = isClose, LayerId = lt[layerName], Plinegen = true, LinetypeScale=3 };//定义不封闭的Polyline
                for (int i = 0; i < ptList.Count; i++)
                {
                    line.AddVertexAt(i, ptList[i], 0, 0, 0);
                }
                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }
          
        }

        public static Polyline GetPolylineByPointList(Database db, List<Point2d> ptList, ref Extents2d ext, string layerName, bool isClose)
        {
            DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;
            Polyline PL = new Polyline();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline line = new Polyline() { Closed = isClose, LayerId = lt[layerName], Plinegen = true, LinetypeScale = 3 };//定义不封闭的Polyline
                for (int i = 0; i < ptList.Count; i++)
                {
                    line.AddVertexAt(i, ptList[i], 0, 0, 0);
                    PL.AddVertexAt(i, ptList[i], 0, 0, 0);
                }
                modelSpace.AppendEntity(PL);
                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }
            return PL;
        }


        public static void AddPolylineByPointListWhitHatch(Database db, List<Point2d> ptList, ref Extents2d ext, string layerName, bool isClose, double scale = 1, double angle=0, string hatchName="ANSI36")
        {
            DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline line = new Polyline() { Closed = isClose, LayerId = lt[layerName], Plinegen = true, LinetypeScale = 5 };//定义不封闭的Polyline
                for (int i = 0; i < ptList.Count; i++)
                {
                    line.AddVertexAt(i, ptList[i], 0, 0, 0);
                }
                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                HatchPloter.HatchEnityWithAngle(db, hatchName, scale, angle, line.ObjectId);
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }

        }

        /// <summary>
        /// 八边形绘图
        /// </summary>
        /// <param name="AnchorPoint">锚点，八边形下中心点</param>
        /// <param name="Height">总高</param>
        /// <param name="Width">总宽</param>
        /// <param name="F1x">下倒角，x方向</param>
        /// <param name="F1y">下倒角，y方向</param>
        /// <param name="F2x">上倒角，x方向</param>
        /// <param name="F2y">上倒角，y方向</param>
        /// <param name="layer">图层</param>
        public static void AddPoly8(Database db,Point2d AnchorPoint, ref Extents2d ext, double Height, double Width,
             string layer,double F1x = 50, double F1y = 50, double F2x = 50, double F2y = 50)
        {
            DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;
        
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                BlockTableRecord modelSpace;
                modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                Polyline PL1 = new Polyline() { Closed = true, LayerId = lt[layer], Plinegen = true };
                Line2d AxisY = new Line2d(AnchorPoint, AnchorPoint.C2D(0, 1));
                Point2d p0, p1, p2, p3, p4, p5, p6, p7;

                p0 = AnchorPoint.C2D(-0.5 * Width + F1x, 0);
                p1 = p0.Mirror(AxisY);
                p2 = p1.C2D(F1x, -F1y);
                p3 = p2.C2D(0, -Height +F1y +F2y);
                p4 = p3.C2D(-F2x, -F2y);
                p5 = p4.Mirror(AxisY);
                p6 = p3.Mirror(AxisY);
                p7 = p2.Mirror(AxisY);
                PL1.AddVertexAt(0, p0, 0, 0, 0);
                PL1.AddVertexAt(1, p1, 0, 0, 0);
                PL1.AddVertexAt(2, p2, 0, 0, 0);
                PL1.AddVertexAt(3, p3, 0, 0, 0);
                PL1.AddVertexAt(4, p4, 0, 0, 0);
                PL1.AddVertexAt(5, p5, 0, 0, 0);
                PL1.AddVertexAt(6, p6, 0, 0, 0);
                PL1.AddVertexAt(7, p7, 0, 0, 0);
                modelSpace.AppendEntity(PL1);
                tr.AddNewlyCreatedDBObject(PL1, true);
                ext = ext.Add(new Extents2d(PL1.Bounds.Value.MinPoint.C2D(), PL1.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }
            
        }

        public static void AddPoly8WhitHatch(Database db, Point2d AnchorPoint, ref Extents2d ext, double Height, double Width,
     string layer, double F1x = 50, double F1y = 50, double F2x = 50, double F2y = 50,double scale = 1, double angle = 0, string hatchName = "ANSI36")
        {
            DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                BlockTableRecord modelSpace;
                modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                Polyline PL1 = new Polyline() { Closed = true, LayerId = lt[layer], Plinegen = true };
                Line2d AxisY = new Line2d(AnchorPoint, AnchorPoint.C2D(0, 1));
                Point2d p0, p1, p2, p3, p4, p5, p6, p7;

                p0 = AnchorPoint.C2D(-0.5 * Width + F1x, 0);
                p1 = p0.Mirror(AxisY);
                p2 = p1.C2D(F1x, -F1y);
                p3 = p2.C2D(0, -Height + F1y + F2y);
                p4 = p3.C2D(-F2x, -F2y);
                p5 = p4.Mirror(AxisY);
                p6 = p3.Mirror(AxisY);
                p7 = p2.Mirror(AxisY);
                PL1.AddVertexAt(0, p0, 0, 0, 0);
                PL1.AddVertexAt(1, p1, 0, 0, 0);
                PL1.AddVertexAt(2, p2, 0, 0, 0);
                PL1.AddVertexAt(3, p3, 0, 0, 0);
                PL1.AddVertexAt(4, p4, 0, 0, 0);
                PL1.AddVertexAt(5, p5, 0, 0, 0);
                PL1.AddVertexAt(6, p6, 0, 0, 0);
                PL1.AddVertexAt(7, p7, 0, 0, 0);
                modelSpace.AppendEntity(PL1);
                tr.AddNewlyCreatedDBObject(PL1, true);
                HatchPloter.HatchEnityWithAngle(db, hatchName, scale, angle, PL1.ObjectId);
                ext = ext.Add(new Extents2d(PL1.Bounds.Value.MinPoint.C2D(), PL1.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }

        }
        public static ObjectId CreatPoly8(Database db,Point2d AnchorPoint, ref Extents2d ext, double Height, double Width,
         string layer, double F1x = 50, double F1y = 50, double F2x = 50, double F2y = 50, double extendscale = 1)
        {
            Height = Height * extendscale;
            Width = Width * extendscale;
            F1x = F1x * extendscale;
            F1y = F1y * extendscale;
            F2x = F2x * extendscale;
            F2y = F2y * extendscale;
            DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;
            ObjectId oid = new ObjectId();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                BlockTableRecord modelSpace;
                modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                Polyline PL1 = new Polyline() { Closed = true, LayerId = lt[layer], Plinegen = true };
                Line2d AxisY = new Line2d(AnchorPoint, AnchorPoint.C2D(0, 1));
                Point2d p0, p1, p2, p3, p4, p5, p6, p7;

                p0 = AnchorPoint.C2D(-0.5 * Width + F1x, 0);
                p1 = p0.Mirror(AxisY);
                p2 = p1.C2D(F1x, -F1y);
                p3 = p2.C2D(0, -Height + F1y + F2y);
                p4 = p3.C2D(-F2x, -F2y);
                p5 = p4.Mirror(AxisY);
                p6 = p3.Mirror(AxisY);
                p7 = p2.Mirror(AxisY);
                PL1.AddVertexAt(0, p0, 0, 0, 0);
                PL1.AddVertexAt(1, p1, 0, 0, 0);
                PL1.AddVertexAt(2, p2, 0, 0, 0);
                PL1.AddVertexAt(3, p3, 0, 0, 0);
                PL1.AddVertexAt(4, p4, 0, 0, 0);
                PL1.AddVertexAt(5, p5, 0, 0, 0);
                PL1.AddVertexAt(6, p6, 0, 0, 0);
                PL1.AddVertexAt(7, p7, 0, 0, 0);
              
                modelSpace.AppendEntity(PL1);
                oid = PL1.ObjectId;
                tr.AddNewlyCreatedDBObject(PL1, true);

                ext = ext.Add(new Extents2d(PL1.Bounds.Value.MinPoint.C2D(), PL1.Bounds.Value.MaxPoint.C2D()));
            
                   tr.Commit();
              
            }
            return oid;
        }

        public static Polyline CreatPoly8Polyline(Database db,Point2d AnchorPoint, ref Extents2d ext, double Height, double Width,
   string layer, double F1x = 50, double F1y = 50, double F2x = 50, double F2y = 50,double extendscale=1)
        {
            Height = Height * extendscale;
            Width = Width * extendscale;
            F1x = F1x * extendscale;
            F1y = F1y * extendscale;
            F2x = F2x * extendscale;
            F2y = F2y * extendscale;
            DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;
            //ObjectId oid = new ObjectId();
            Polyline PL1 = new Polyline() { Closed = true,Plinegen = true };
            //using (Transaction tr = db.TransactionManager.StartTransaction())
            //{
            //    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
            //    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
            //    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
            //    BlockTableRecord modelSpace;
            //    modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
            //    PL1 = new Polyline() { Closed = true, LayerId = lt[layer], Plinegen = true };


            //    modelSpace.AppendEntity(PL1);

            //    tr.AddNewlyCreatedDBObject(PL1, true);
            //    PL1
            //    ext = ext.Add(new Extents2d(PL1.Bounds.Value.MinPoint.C2D(), PL1.Bounds.Value.MaxPoint.C2D()));


            //}
            Line2d AxisY = new Line2d(AnchorPoint, AnchorPoint.C2D(0, 1));
            Point2d p0, p1, p2, p3, p4, p5, p6, p7;

            p0 = AnchorPoint.C2D(-0.5 * Width + F1x, 0);
            p1 = p0.Mirror(AxisY);
            p2 = p1.C2D(F1x, -F1y);
            p3 = p2.C2D(0, -Height + F1y + F2y);
            p4 = p3.C2D(-F2x, -F2y);
            p5 = p4.Mirror(AxisY);
            p6 = p3.Mirror(AxisY);
            p7 = p2.Mirror(AxisY);
            PL1.AddVertexAt(0, p0, 0, 0, 0);
            PL1.AddVertexAt(1, p1, 0, 0, 0);
            PL1.AddVertexAt(2, p2, 0, 0, 0);
            PL1.AddVertexAt(3, p3, 0, 0, 0);
            PL1.AddVertexAt(4, p4, 0, 0, 0);
            PL1.AddVertexAt(5, p5, 0, 0, 0);
            PL1.AddVertexAt(6, p6, 0, 0, 0);
            PL1.AddVertexAt(7, p7, 0, 0, 0);
            return PL1;
        }

        /// <summary>
        /// 添加四角圆弧多边形
        /// </summary>
        /// <param name="AnchorPoint">插入点</param>
        /// <param name="Height">高</param>
        /// <param name="Width">宽</param>
        /// <param name="layer">图层</param>
        /// <param name="ArcR">圆弧半径</param>
        /// <returns></returns>
        public static void AddArcPoly(Database db,Point2d AnchorPoint,ref Extents2d ext, double Height, double Width,
          string layer, double ArcR = 50)
        {          
            //Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline PL1 = new Polyline() { Closed = true, LayerId = lt[layer], Plinegen = true };

                Line2d AxisY = new Line2d(AnchorPoint, AnchorPoint.C2D(0, 1));
                Point2d p0, p1, p2, p3, p4, p5, p6, p7;

                p0 = AnchorPoint.C2D(-0.5 * Width + ArcR, 0);
                p1 = p0.Mirror(AxisY);
                p2 = p1.C2D(ArcR, -ArcR);
                p3 = p2.C2D(0, -Height + ArcR + ArcR);
                p4 = p3.C2D(-ArcR, -ArcR);
                p5 = p4.Mirror(AxisY);
                p6 = p3.Mirror(AxisY);
                p7 = p2.Mirror(AxisY);
                AddLine(db,p0, p1, ref ext, layer);
                ;
                AddLine(db,p2, p3, ref ext, layer);

                AddLine(db,p4, p5, ref ext, layer);

                AddLine(db,p6, p7, ref ext, layer);

                Arc arc2 = new Arc(p0.C3D(0, -ArcR), ArcR, GeTools.DegreeToRadian(90), GeTools.DegreeToRadian(90 + 90)) { LayerId = lt[layer] };
                modelSpace.AppendEntity(arc2);
                tr.AddNewlyCreatedDBObject(arc2, true);
                ext = ext.Add(new Extents2d(arc2.Bounds.Value.MinPoint.C2D(), arc2.Bounds.Value.MaxPoint.C2D()));
                arc2 = new Arc(p1.C3D(0, -ArcR), ArcR, GeTools.DegreeToRadian(90 - 90), GeTools.DegreeToRadian(90)) { LayerId = lt[layer] };
                modelSpace.AppendEntity(arc2);
                tr.AddNewlyCreatedDBObject(arc2, true);
                ext = ext.Add(new Extents2d(arc2.Bounds.Value.MinPoint.C2D(), arc2.Bounds.Value.MaxPoint.C2D()));
                arc2 = new Arc(p4.C3D(0, ArcR), ArcR, GeTools.DegreeToRadian(-90), GeTools.DegreeToRadian(-90 + 90)) { LayerId = lt[layer] };
                modelSpace.AppendEntity(arc2);
                tr.AddNewlyCreatedDBObject(arc2, true);
                ext = ext.Add(new Extents2d(arc2.Bounds.Value.MinPoint.C2D(), arc2.Bounds.Value.MaxPoint.C2D()));
                arc2 = new Arc(p5.C3D(0, ArcR), ArcR, GeTools.DegreeToRadian(-90 - 90), GeTools.DegreeToRadian(-90)) { LayerId = lt[layer] };


                modelSpace.AppendEntity(arc2);
                tr.AddNewlyCreatedDBObject(arc2, true);
                ext = ext.Add(new Extents2d(arc2.Bounds.Value.MinPoint.C2D(), arc2.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }
      
        }

        public static void AddHalfCicle(Database db, Point2d AnchorPoint, ref Extents2d ext,string layer, double ArcR = 50,bool isUpper=false)
        {
            //Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

                if (isUpper)
                {
                    Arc arc2 = new Arc(AnchorPoint.C3D(), ArcR, GeTools.DegreeToRadian(0), GeTools.DegreeToRadian(90 + 90)) { LayerId = lt[layer] };
                    modelSpace.AppendEntity(arc2);
                    tr.AddNewlyCreatedDBObject(arc2, true);
                    ext = ext.Add(new Extents2d(arc2.Bounds.Value.MinPoint.C2D(), arc2.Bounds.Value.MaxPoint.C2D()));
                }
                else
                {
                    Arc arc2 = new Arc(AnchorPoint.C3D(), ArcR, GeTools.DegreeToRadian(180), GeTools.DegreeToRadian(360)) { LayerId = lt[layer] };
                    modelSpace.AppendEntity(arc2);
                    tr.AddNewlyCreatedDBObject(arc2, true);
                    ext = ext.Add(new Extents2d(arc2.Bounds.Value.MinPoint.C2D(), arc2.Bounds.Value.MaxPoint.C2D()));
                }
                tr.Commit();
            }

        }

        public static Arc AddAreWithHatch(Database db, Point2d AnchorPoint, ref Extents2d ext, string layer,double scale, double ArcR = 50,double startAng=0,double endAng=180)
        {
            //Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;


                Arc arc2 = new Arc(AnchorPoint.C3D(), ArcR, GeTools.DegreeToRadian(startAng), GeTools.DegreeToRadian(endAng)) { LayerId = lt[layer] };
                modelSpace.AppendEntity(arc2);

                Arc arc = new Arc(AnchorPoint.C3D(), ArcR, GeTools.DegreeToRadian(startAng), GeTools.DegreeToRadian(endAng)) { LayerId = lt[layer] };
                modelSpace.AppendEntity(arc);

                Line line = new Line(arc2.StartPoint, arc2.EndPoint);
                modelSpace.AppendEntity(line);
                ObjectId[] ids = new ObjectId[] { arc2.ObjectId, line.ObjectId };
                tr.AddNewlyCreatedDBObject(arc2, true);
                HatchPloter.HatchEnity(db, "AR-CONC", 30, 0, ids);
                ext = ext.Add(new Extents2d(arc2.Bounds.Value.MinPoint.C2D(), arc2.Bounds.Value.MaxPoint.C2D()));

                tr.Commit();
                return arc;
            }

        }

        /// <summary>
        /// 绘制四边形
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="upcenter"></param>
        /// <param name="wl"></param>
        /// <param name="wr"></param>
        /// <param name="h"></param>
        /// <param name="layer"></param>
        /// <param name="isDrawing"></param>
        /// <returns></returns>
        public static void AddPloy4(Database db,  Point2d upcenter, double wl, double wr, double h,ref Extents2d ext, string layer)
        {
            DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace= tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline PL1 = new Polyline() { Closed = true, LayerId = lt[layer], Plinegen = true,LinetypeScale=5 };
                Point2d ptop, pbot;
                Point2d p11, p12, p13, p14;
                ptop = upcenter;
                pbot = upcenter.C2D(0, -h);
                p11 = ptop.C2D(-wl);
                p12 = pbot.C2D(-wl);
                p13 = ptop.C2D(wr);
                p14 = pbot.C2D(wr);
                PL1.AddVertexAt(0, p12, 0, 0, 0);
                PL1.AddVertexAt(1, pbot, 0, 0, 0);
                PL1.AddVertexAt(2, p14, 0, 0, 0);

                PL1.AddVertexAt(3, p13, 0, 0, 0);
                PL1.AddVertexAt(4, ptop, 0, 0, 0);
                PL1.AddVertexAt(5, p11, 0, 0, 0);

                modelSpace.AppendEntity(PL1);
                tr.AddNewlyCreatedDBObject(PL1, true);
                ext = ext.Add(new Extents2d(PL1.Bounds.Value.MinPoint.C2D(), PL1.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }
            
        }

        public static void AddPloy4WithHatch(Database db,Point2d upcenter, double wl, double wr, double h, ref Extents2d ext, string layer)
        {
            DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline PL1 = new Polyline() { Closed = true, LayerId = lt[layer], Plinegen = true, LinetypeScale = 5 };
                Point2d ptop, pbot;
                Point2d p11, p12, p13, p14;
                ptop = upcenter;
                pbot = upcenter.C2D(0, -h);
                p11 = ptop.C2D(-wl);
                p12 = pbot.C2D(-wl);
                p13 = ptop.C2D(wr);
                p14 = pbot.C2D(wr);
                PL1.AddVertexAt(0, p12, 0, 0, 0);
                PL1.AddVertexAt(1, pbot, 0, 0, 0);
                PL1.AddVertexAt(2, p14, 0, 0, 0);

                PL1.AddVertexAt(3, p13, 0, 0, 0);
                PL1.AddVertexAt(4, ptop, 0, 0, 0);
                PL1.AddVertexAt(5, p11, 0, 0, 0);

                modelSpace.AppendEntity(PL1);
                tr.AddNewlyCreatedDBObject(PL1, true);
                HatchPloter.HatchEnity(db, "ANSI31", 5, 0, PL1.ObjectId);
                ext = ext.Add(new Extents2d(PL1.Bounds.Value.MinPoint.C2D(), PL1.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }

        }

        public static void AddPloy4WithHatch(Database db, Point2d upcenter, double wl, double wr, double h, ref Extents2d ext, string layer,string hatchName= "ANSI31",double scale=50)
        {
            DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline PL1 = new Polyline() { Closed = true, LayerId = lt[layer], Plinegen = true, LinetypeScale = 5 };
                Point2d ptop, pbot;
                Point2d p11, p12, p13, p14;
                ptop = upcenter;
                pbot = upcenter.C2D(0, -h);
                p11 = ptop.C2D(-wl);
                p12 = pbot.C2D(-wl);
                p13 = ptop.C2D(wr);
                p14 = pbot.C2D(wr);
                PL1.AddVertexAt(0, p12, 0, 0, 0);
                PL1.AddVertexAt(1, pbot, 0, 0, 0);
                PL1.AddVertexAt(2, p14, 0, 0, 0);

                PL1.AddVertexAt(3, p13, 0, 0, 0);
                PL1.AddVertexAt(4, ptop, 0, 0, 0);
                PL1.AddVertexAt(5, p11, 0, 0, 0);

                modelSpace.AppendEntity(PL1);
                tr.AddNewlyCreatedDBObject(PL1, true);
                HatchPloter.HatchEnity(db, hatchName, scale, 0, PL1.ObjectId);
                ext = ext.Add(new Extents2d(PL1.Bounds.Value.MinPoint.C2D(), PL1.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }

        }
        /// <summary>
        /// 绘制实心圆
        /// </summary>
        /// <param name="pt">圆点</param>
        /// <param name="radius">半径</param>
        /// <returns></returns>
        public static void AddSolidCircle(Database db,Point2d pt,ref Extents2d ext, double radius,string layerName= "H标注")
        {
            //DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                Circle circle = new Circle(pt.C3D(), Vector3d.ZAxis, radius) { LayerId = lt[layerName] };
                circle.SetDatabaseDefaults();//默认参数
                                             // 创建图案填充对象并添加到块表记录中   
                Hatch hatch = new Hatch();
                modelSpace.AppendEntity(hatch);
                tr.AddNewlyCreatedDBObject(hatch, true);
                //res.Add(circle);


                modelSpace.AppendEntity(circle);
                tr.AddNewlyCreatedDBObject(circle, true);
                ext = ext.Add(new Extents2d(circle.Bounds.Value.MinPoint.C2D(), circle.Bounds.Value.MaxPoint.C2D()));
                hatch.LayerId = lt[layerName];

                hatch.SetDatabaseDefaults();
                hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID"); //设置填充图案
                // 设置关联
                hatch.Associative = true;
                // 添加圆到一个 ObjectID 数组中去 
                ObjectIdCollection acObjIdColl = new ObjectIdCollection();
                acObjIdColl.Add(circle.ObjectId);
                hatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl);
                hatch.EvaluateHatch(true);
         
                tr.Commit();
            }
            //return res;
        }
       
        public static void AddCircle(Database db,Point2d pt, double radius,ref Extents2d ext, string layerName = "H标注",double scale=1)
        {
            //DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Circle circle = new Circle(pt.C3D(), Vector3d.ZAxis, radius) { LayerId = lt[layerName] ,LinetypeScale= scale };
                circle.SetDatabaseDefaults();//默认参数
                modelSpace.AppendEntity(circle);
                tr.AddNewlyCreatedDBObject(circle, true);
                ext = ext.Add(new Extents2d(circle.Bounds.Value.MinPoint.C2D(), circle.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();

            }
            //return res;
        }
        public static void CreateBreakLine(Database db, ref Extents2d ext, Point2d stPoint, Point2d endPoint, double scale = 20)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;
                recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                #region
                Point2d centerPt = GeTools.MidPoint(stPoint.C3D(), endPoint.C3D()).C2D();
                //画线
                Polyline line = new Polyline() { Closed = false, Layer = "H细线", ColorIndex = 1 };//定义不封闭的Polyline 平面虚线
                if (stPoint.Y > endPoint.Y && stPoint.X == endPoint.X)
                {
                    line.AddVertexAt(0, stPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(0, 1.7941 * scale * 0.05), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(-2.4148 * scale * 0.05, 1.147 * scale * 0.05), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(2.4148 * scale * 0.05, -1.147 * scale * 0.05), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(0, -1.7941 * scale * 0.05), 0, 0, 0);
                    line.AddVertexAt(6, endPoint, 0, 0, 0);
                }
                else if (stPoint.Y <= endPoint.Y && stPoint.X == endPoint.X)
                {
                    line.AddVertexAt(0, endPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(0, 1.7941 * scale * 0.05), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(-2.4148 * scale * 0.05, 1.147 * scale * 0.05), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(2.4148 * scale * 0.05, -1.147 * scale * 0.05), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(0, -1.7941 * scale * 0.05), 0, 0, 0);
                    line.AddVertexAt(6, stPoint, 0, 0, 0);
                }

                if (stPoint.X > endPoint.X && stPoint.Y == endPoint.Y)
                {
                    line.AddVertexAt(0, stPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(1.7941 * scale * 0.05, 0), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(1.147 * scale * 0.05, -2.4148 * scale * 0.05), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(-1.147 * scale * 0.05, 2.4148 * scale * 0.05), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(-1.7941 * scale * 0.05, 0), 0, 0, 0);
                    line.AddVertexAt(6, endPoint, 0, 0, 0);
                }
                else if (stPoint.X <= endPoint.X && stPoint.Y == endPoint.Y)
                {
                    line.AddVertexAt(0, endPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(1.7941 * scale * 0.05, 0), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(1.147 * scale * 0.05, -2.4148 * scale * 0.05), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(-1.147 * scale * 0.05, 2.4148 * scale * 0.05), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(-1.7941 * scale * 0.05, 0), 0, 0, 0);
                    line.AddVertexAt(6, stPoint, 0, 0, 0);
                }
                recorder.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);

                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));

                #endregion
                tr.Commit();
            }
        }

        public static void CreateBreakLineP(Database db, ref Extents2d ext, Point2d stPoint, Point2d endPoint, double scale = 1,string Layer = "H细线")
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;
                recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                #region
                Point2d centerPt = GeTools.MidPoint(stPoint.C3D(), endPoint.C3D()).C2D();
                //画线
                Polyline line = new Polyline() { Closed = false, LayerId = lt[Layer] };//定义不封闭的Polyline 平面虚线
                if (stPoint.Y > endPoint.Y && stPoint.X == endPoint.X)
                {
                    line.AddVertexAt(0, stPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(0, 1.5 * scale ), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(-1.5 * scale , 1 * scale ), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(1.5 * scale , -1 * scale ), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(0, -1.5 * scale ), 0, 0, 0);
                    line.AddVertexAt(6, endPoint, 0, 0, 0);
                }
                else if (stPoint.Y <= endPoint.Y && stPoint.X == endPoint.X)
                {
                    line.AddVertexAt(0, endPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(0, 1.5 * scale ), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(-1.5 * scale , 1 * scale ), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(1.5 * scale , -1 * scale ), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(0, -1.5 * scale ), 0, 0, 0);
                    line.AddVertexAt(6, stPoint, 0, 0, 0);
                }

                if (stPoint.X > endPoint.X && stPoint.Y == endPoint.Y)
                {
                    line.AddVertexAt(0, stPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(1.5 * scale , 0), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(1 * scale , -1.5 * scale ), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(-1 * scale, 1.5 * scale), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(-1.5 * scale , 0), 0, 0, 0);
                    line.AddVertexAt(6, endPoint, 0, 0, 0);
                }
                else if (stPoint.X <= endPoint.X && stPoint.Y == endPoint.Y)
                {
                    line.AddVertexAt(0, endPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(1.5 * scale , 0), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(1 * scale , -1.5 * scale), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(-1 * scale , 1.5 * scale ), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(-1.5 * scale, 0), 0, 0, 0);
                    line.AddVertexAt(6, stPoint, 0, 0, 0);
                }
                recorder.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);

                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));

                #endregion
                tr.Commit();
            }
        }

        public static void CreateBreakLineP2PaPer(Database db, ref Extents2d ext, Point2d stPoint, Point2d endPoint, double scale = 1, string Layer = "H细线")
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;
                recorder = tr.GetObject(blockTbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                #region
                Point2d centerPt = GeTools.MidPoint(stPoint.C3D(), endPoint.C3D()).C2D();
                //画线
                Polyline line = new Polyline() { Closed = false, LayerId = lt[Layer] };//定义不封闭的Polyline 平面虚线
                if (stPoint.Y > endPoint.Y && stPoint.X == endPoint.X)
                {
                    line.AddVertexAt(0, stPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(0, 1.5 * scale), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(-1.5 * scale, 1 * scale), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(1.5 * scale, -1 * scale), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(0, -1.5 * scale), 0, 0, 0);
                    line.AddVertexAt(6, endPoint, 0, 0, 0);
                }
                else if (stPoint.Y <= endPoint.Y && stPoint.X == endPoint.X)
                {
                    line.AddVertexAt(0, endPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(0, 1.5 * scale), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(-1.5 * scale, 1 * scale), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(1.5 * scale, -1 * scale), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(0, -1.5 * scale), 0, 0, 0);
                    line.AddVertexAt(6, stPoint, 0, 0, 0);
                }

                if (stPoint.X > endPoint.X && stPoint.Y == endPoint.Y)
                {
                    line.AddVertexAt(0, stPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(1.5 * scale, 0), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(1 * scale, -1.5 * scale), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(-1 * scale, 1.5 * scale), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(-1.5 * scale, 0), 0, 0, 0);
                    line.AddVertexAt(6, endPoint, 0, 0, 0);
                }
                else if (stPoint.X <= endPoint.X && stPoint.Y == endPoint.Y)
                {
                    line.AddVertexAt(0, endPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(1.5 * scale, 0), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(1 * scale, -1.5 * scale), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(-1 * scale, 1.5 * scale), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(-1.5 * scale, 0), 0, 0, 0);
                    line.AddVertexAt(6, stPoint, 0, 0, 0);
                }
                recorder.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);

                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));

                #endregion
                tr.Commit();
            }
        }
        /// <summary>
        /// 添加Line线
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="stPoint"></param>
        /// <param name="endPoint"></param>
        /// <param name="layer"></param>
        /// <returns></returns>

        public static Line AddLine(Database db ,Point2d stPoint, Point2d endPoint, ref Extents2d ext, string layer)
        {

            //Database db = HostApplicationServices.WorkingDatabase;
            Line line1 = new Line(stPoint.C3D(), endPoint.C3D());
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Line line = new Line(stPoint.C3D(), endPoint.C3D());
                line.LayerId = lt[layer];

                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }
            return line1;
        }


        //public static Line AddLine(db,Database db, Point2d stPoint, Point2d endPoint, ref Extents2d ext,string layer)
        //{
         
        //    Database db = HostApplicationServices.WorkingDatabase;
        //    Line line1 = new Line(stPoint.C3D(), endPoint.C3D());
        //    using (Transaction tr = db.TransactionManager.StartTransaction())
        //    {
        //        BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
        //        BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
        //            OpenMode.ForWrite) as BlockTableRecord;
        //        TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
        //        LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
        //        Line line = new Line(stPoint.C3D(), endPoint.C3D());
        //        line.LayerId = lt[layer];
            
        //        modelSpace.AppendEntity(line);
        //        tr.AddNewlyCreatedDBObject(line, true);
        //        ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
        //        tr.Commit();
        //    }
        //    return line1;
        //}
        public static Line AddScaleLine(Database db, Point2d stPoint, Point2d endPoint, ref Extents2d ext, string layer, double scale = 1)
        {

            //Database db = HostApplicationServices.WorkingDatabase;
            Line line = new Line(stPoint.C3D(), endPoint.C3D());
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                line.LayerId = lt[layer];
                line.LinetypeScale = scale;
                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }
            return line;
        }
        public static Line AddLineWithColor(Database db, Point2d stPoint, Point2d endPoint, ref Extents2d ext, string layer,  int colorindex)
        {

            //Database db = HostApplicationServices.WorkingDatabase;
            Line line = new Line(stPoint.C3D(), endPoint.C3D());
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                line.LayerId = lt[layer];
                line.ColorIndex = colorindex;
                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }
            return line;
        }

        public static void AddUpperArcBreakLine(Database db, Point2d pt, ref Extents2d ext,double L = 100 ,string Layer="H细线")
        {
            //Database db = HostApplicationServices.WorkingDatabase;
            //DBObjectCollection res = new DBObjectCollection();
            double arcRadius = L * 0.5;
            #region      
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                #region  圆弧
             
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                double arcAngle = 60;

                Arc arc1 = new Arc(pt.C3D(-arcRadius + arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)),  arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2))),
                    arcRadius, GeTools.DegreeToRadian(240), GeTools.DegreeToRadian(240 + arcAngle))
                { LayerId = lt[Layer] };
                modelSpace.AppendEntity(arc1);
                tr.AddNewlyCreatedDBObject(arc1, true);
                ext = ext.Add(new Extents2d(arc1.Bounds.Value.MinPoint.C2D(), arc1.Bounds.Value.MaxPoint.C2D()));

                Arc arc2 = new Arc(pt.C3D((arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2))), -(arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2)))),
                    arcRadius, GeTools.DegreeToRadian(60), GeTools.DegreeToRadian(60 + arcAngle))
                { LayerId = lt[Layer] };
                modelSpace.AppendEntity(arc2);
                tr.AddNewlyCreatedDBObject(arc2, true);
                ext = ext.Add(new Extents2d(arc2.Bounds.Value.MinPoint.C2D(), arc2.Bounds.Value.MaxPoint.C2D()));
                //res.Add(arc2);

                Arc arc3 = new Arc(pt.C3D((arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2))), arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2))),
                    arcRadius, GeTools.DegreeToRadian(240), GeTools.DegreeToRadian(240 + arcAngle))
                { LayerId = lt[Layer] };

                //res.Add(arc3);

                modelSpace.AppendEntity(arc3);
                tr.AddNewlyCreatedDBObject(arc3, true);
                ext = ext.Add(new Extents2d(arc3.Bounds.Value.MinPoint.C2D(), arc3.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
                #endregion
            }
            #endregion
            //return res;
        }

        public static void AddBottomArcBreakLine(Database db, Point2d pt,ref Extents2d ext, double L = 100,string Layer = "H细线")
        {
            //Database db = HostApplicationServices.WorkingDatabase;
            //DBObjectCollection res = new DBObjectCollection();
            #region 
            double arcRadius = L* 0.5;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                Point3dCollection pts = new Point3dCollection();
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                #region  圆弧
                double arcAngle = 60;
                double cosH = arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2));
                // 绘制弧线
                Arc arc1 = new Arc(pt.C3D(-arcRadius + arcRadius / 2, -cosH), arcRadius, GeTools.DegreeToRadian(60), GeTools.DegreeToRadian(60 + arcAngle)) { LayerId = lt[Layer] };
                modelSpace.AppendEntity(arc1);
                tr.AddNewlyCreatedDBObject(arc1, true);
                ext = ext.Add(new Extents2d(arc1.Bounds.Value.MinPoint.C2D(), arc1.Bounds.Value.MaxPoint.C2D()));



                Arc arc2 = new Arc(pt.C3D((arcRadius / 2), -cosH), arcRadius, GeTools.DegreeToRadian(60), GeTools.DegreeToRadian(60 + arcAngle)) { LayerId = lt[Layer] };
                modelSpace.AppendEntity(arc2);
                tr.AddNewlyCreatedDBObject(arc2, true);
                ext = ext.Add(new Extents2d(arc2.Bounds.Value.MinPoint.C2D(), arc2.Bounds.Value.MaxPoint.C2D()));


                Arc arc3 = new Arc(pt.C3D(-arcRadius + arcRadius / 2, cosH), arcRadius, GeTools.DegreeToRadian(240), GeTools.DegreeToRadian(240 + arcAngle)) { Layer = Layer };

                modelSpace.AppendEntity(arc3);
                tr.AddNewlyCreatedDBObject(arc3, true);
                ext = ext.Add(new Extents2d(arc3.Bounds.Value.MinPoint.C2D(), arc3.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
                #endregion
            }
            #endregion
            //return res;
        }


        /// <summary>
        /// 环形阵列
        /// </summary>
        /// <param name="ent">图形对象</param>
        /// <param name="num">图形数量</param>
        /// <param name="degree">中心点到各个图形的夹角</param>
        /// <param name="center">中心点</param>
        /// <returns>List</returns>
        public static void ArrayPolarEntity(this Entity ent, Database db,int num, double degree, Point3d center)
        {
            if (ent.IsNewObject == true)
            {
                // 声明一个List集合 用于返回
                List<Entity> entList = new List<Entity>();
                //Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    // 打开块表
                    BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                    // 打开块表记录
                    BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                    degree = degree > 360 ? 360 : degree;
                    degree = degree < -360 ? -360 : degree;
                    int divAngnum = num - 1;
                    if (degree == 360 || degree == -360)
                    {
                        divAngnum = num;
                    }
                    for (int i = 0; i < num; i++)
                    {
                        Matrix3d mt = Matrix3d.Rotation(GeTools.DegreeToRadian(i * degree / divAngnum), Vector3d.ZAxis, center);
                        Entity entA = ent.GetTransformedCopy(mt);
                        btr.AppendEntity(entA);
                        trans.AddNewlyCreatedDBObject(entA, true);
                        entList.Add(entA);
                    }
                    trans.Commit();
                }
             
            }
         
        }

        public static void AddPloy4ToPaper(Database db,Point2d upcenter, double wl, double wr, double h,  string layer="H细线")
        {
            DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline PL1 = new Polyline() { Closed = true, LayerId = lt[layer], Plinegen = true };
                Point2d ptop, pbot;
                Point2d p11, p12, p13, p14;
                ptop = upcenter;
                pbot = upcenter.C2D(0, -h);
                p11 = ptop.C2D(-wl);
                p12 = pbot.C2D(-wl);
                p13 = ptop.C2D(wr);
                p14 = pbot.C2D(wr);
                PL1.AddVertexAt(0, p12, 0, 0, 0);
                PL1.AddVertexAt(1, pbot, 0, 0, 0);
                PL1.AddVertexAt(2, p14, 0, 0, 0);

                PL1.AddVertexAt(3, p13, 0, 0, 0);
                PL1.AddVertexAt(4, ptop, 0, 0, 0);
                PL1.AddVertexAt(5, p11, 0, 0, 0);

                modelSpace.AppendEntity(PL1);
                tr.AddNewlyCreatedDBObject(PL1, true);
                //ext = ext.Add(new Extents2d(PL1.Bounds.Value.MinPoint.C2D(), PL1.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }

        }
    }


    public enum ArrowDirection
    {
        East,//东方      右
        West, //西方     左
        North ,// 北方   上
        South, //南方    下
    }

    public enum eDirection
    {
        E_LEFT,
        E_RIGHT,
        E_TOP,
        E_BOTTOM
    }

    public enum eArrow
    {
        E_ARROW_NULL,                   // 无箭头
        E_ARROW_LEFT_ARROW,             // 左边箭头
        E_ARROW_RIGHT_ARROW,            // 左边箭头
        E_ARROW_DOUBLE_SIDE_ARROW,      // 双向箭头
    }
}
